﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for EditEventWindow.xaml
    /// </summary>
    public partial class VWPLANVKP : UserControl
    {
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        public INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }

        public DataRow dwVKPOS
        {
            get
            {
                return vm.dwVKP;
            }
        }



        bool IsLoaded = false;
        VYGRONAV95 vm;
        SETINTEXDB SetMaster
        {
            get
            {
                return vm.SetMaster;
            }
        }
        SETINTEXDB SetEnv
        {
            get
            {
                return vm.SetEnv;
            }
        }
        SETINTEXDB SetDetail
        {
            get
            {
                return vm.SetDetail;
            }
        }
        ITXEXRPT ITXEXRPT;

        private readonly TaskScheduler _sync = TaskScheduler.FromCurrentSynchronizationContext();
        public VWPLANVKP()
        {
            InitializeComponent();
            this.Loaded += Windows_Loaded;
            ITXEXRPT = new ITXEXRPT();
            ITXEXRPT.Root = "C:\\IntexReport";
            ITXEXRPT.strCompany = app.company;
            ITXEXRPT.PrintType = "OpenExcel";
        }
        public delegate DataSet AsyncRunHandle(object p1 = null, object p2 = null, object p3 = null, object p4 = null, object p5 = null);
        public void AsyncRun(string TaskType, object p1 = null, object p2 = null, object p3 = null, object p4 = null, object p5 = null)
        {
            AsyncRunHandle fHandle = null;
            AsyncCallback callback = null;
            IAsyncResult aResult = null;
            WaitHandle waitHandle = null;
            switch (TaskType)
            {
                case "YGRAV95":
                    //fHandle = new AsyncRunHandle(INTEXBL.YGRAV95);
                    break;
            }
            callback = new AsyncCallback(this.AsyncCallbackImpl);
            aResult = fHandle.BeginInvoke(p1, p2, p3, p4, p5, callback, fHandle);
            waitHandle = aResult.AsyncWaitHandle;
            waitHandle.WaitOne();
        }
        public void AsyncCallbackImpl(IAsyncResult run)
        {
            try
            {
                AsyncRunHandle handle = run.AsyncState as AsyncRunHandle;
                DataSet SResult = handle.EndInvoke(run);
                switch (handle.Method.Name)
                {
                    case "YGRAV95": 
                        Task.Factory.StartNew(() => vm.LoadData(null), new CancellationTokenSource().Token, TaskCreationOptions.None, _sync).Wait();
                        break;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        string filter = "";
        DataRow dwRawYarn = null;
        void Windows_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            IsLoaded = true;
            vm = this.DataContext as VYGRONAV95;
        }


        private void btnConvert_Click(object sender, RoutedEventArgs e)
        {
            DataSet SResult;
            SResult = new DataSet();
            SResult.Merge(SetDetail);
            EventDispatcher.Instance.RaiseStatus("YGRAV95启动", StatusEventType.StopProgress);
            string path = @"C:\IntexReport\Data\YGRAV95";
            try
            {
                SResult.Tables["ABPOS"].TableName = "ABPOS_EXT";
                INTEXBL.Save2Xml(SResult, path);
                //AsyncRun("YGRAV95","YGR", path);
                INTEXBL.AutoConvert(path, "YGRAV95");
                vm.ABPLFD = "X";
                DataRow[] rows = null;

                filter = "VKPAUFKRS='" + vm.KRS + "' AND VKPAUFLFD='" + vm.LFD + "' AND VKPAUFPOS='" + vm.POS + "'";

                rows = SetEnv.Tables["VKPOS"].Select(filter);
                foreach (DataRow row in rows)
                {
                    row["VKPRESERV"] = row["VKPMENGE1"];
                }
                rows = SetMaster.Tables["VKPOS"].Select(filter);
                foreach (DataRow row in rows)
                {
                    row["VKPRESERV"] = row["VKPMENGE1"];
                } 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }


        private void btnDeleteABON_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string krs = dwVKPOS["VKPAUFKRS"].ToString();
                string lfd = dwVKPOS["VKPAUFLFD"].ToString();
                string pos = dwVKPOS["VKPAUFPOS"].ToString();

                vm.ITXABPLANData.DeleteHAV95(krs, lfd, pos);
                DataRow dwData = SetEnv.ICC_QUERYTB.NewRow();
                dwData["VKPMANDANT"] = app.Mandant;
                dwData["VKPAUFKRS"] = krs;
                dwData["VKPAUFLFD"] = lfd;
                dwData["VKPAUFPOS"] = pos;
                dwData["C20"] = 0;
                INTEXDBC.UpdateVKPOS(dwData, app.company, 6);
                dwData["GAMANDANT"] = app.Mandant;
                dwData["GAAUFKRS"] = krs;
                dwData["GAAUFLFD"] = lfd;
                dwData["GAAUFPOS"] = pos;
                dwData["C20"] = 0;
                INTEXDBC.UpdateGAAUF(dwData, 6);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnPrintAV95_Click(object sender, RoutedEventArgs e)
        {
            ITXEXRPT.dsSource = SetDetail;
            ITXEXRPT.dsSource.Merge(SetEnv);
            ITXEXRPT.DocType = "JLTONAV95";
            ITXEXRPT.strExcelFile = @"\JLT\JLTONAV95.xls";
            string filename = ITXEXRPT.WriteExcelReport();
        }


        private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            TextBox tb = sender as TextBox;
            tb.Text = tb.Text.Trim();
        }
    }
}
